package com.bening.smsapp.util;

import java.util.*;
import java.util.Date;
import java.sql.*;
import java.io.*;

import org.apache.log4j.Logger;

import com.bening.smsapp.factory.*;
import com.bening.smsapp.parser.AsciiParser;
import com.bening.smsapp.bean.FileSource;
import com.bening.smsapp.bean.PathSource;
import com.bening.smsapp.bootstrap.BootConfigParameters;
import com.bening.smsapp.constant.PropertiesConstant;
import com.bening.smsapp.constant.SmsAppConstant;

public class SmsAppTools {
	static BootConfigParameters params = BootConfigParameters.getInstance();
	
    public static String getTimestamp()
    {
        StringBuffer sb = new StringBuffer();

        Calendar now = new GregorianCalendar();
        sb.append( leftPadding( now.get( Calendar.YEAR ), "0", 4 ) );
        sb.append( leftPadding( ( now.get( Calendar.MONTH ) + 1 ), "0", 2 ) );
        sb.append( leftPadding( now.get( Calendar.DAY_OF_MONTH ), "0", 2 ) );
        sb.append( leftPadding( now.get( Calendar.HOUR_OF_DAY ), "0", 2 ) );
        sb.append( leftPadding( now.get( Calendar.MINUTE ), "0", 2 ) );
        sb.append( leftPadding( now.get( Calendar.SECOND ), "0", 2 ) );

        return sb.toString();
    }
    public static String leftPadding( int value, String padding, int length )
    {
        return leftPadding( value + "", padding, length );
    }

    public static String leftPadding( String value, String padding, int length )
    {
        StringBuffer sb = new StringBuffer();
        int valueLength = value.length();

        if( length < 1 )
        {
            throw new IllegalArgumentException( "StringTools: cannot use length <= 0" );
        }

        int dif = length - valueLength;
        if( dif > 0 )
        {
            for( int i = 0; i < dif; i++ )
            {
                sb.append( padding );
            }
        }
        sb.append( value );

        return sb.toString();
    }

    public static List getListDifference( List olderList, List currentList )
    {
    	
        	TreeSet treeSet = new TreeSet( olderList );
    		List result = new ArrayList();
	        int currentListSize = currentList.size();
	        
	        for( int i = 0; i < currentListSize; i++ )
	        {
	        	FileSource f = (FileSource)currentList.get(i);
	        	if(olderList.size()!=0) {
	
	            String element = ( String )f.getFileName();
		            if( !treeSet.contains( element ) )
		            {
		            	result.add(f);
		            }
	            }
	            else {
	            	result = currentList;
	            }
	        }
	        return result;
    	
    }
    public static void InsertEventLog( int eventId, String eventDesc, int sourcePathId  )  {
        Connection conn;
        PreparedStatement prepStmt = null;
        String sql = params.getQueryProperties().getProperty(PropertiesConstant.SQL_INSERT_EVENT_LOG);
        conn = SmsAppFactory.getDbConnection();
        try  {
	        prepStmt = conn.prepareStatement(sql);
	        prepStmt.setInt(1, sourcePathId);
	        prepStmt.setInt(2, eventId);
	        prepStmt.setString(3, eventDesc);
	        prepStmt.executeUpdate();
	        prepStmt.close();
	        conn.close();
        }catch(SQLException s) {
        	s.printStackTrace();
        }
    }
    public static void updateFileStatus( String filename, int constStatus) throws SQLException {
    	String sql = "";
    	try {
    	switch(constStatus) {
    		case SmsAppConstant.FLAG_PARSE_READY:
    			sql = params.getQueryProperties().getProperty(PropertiesConstant.SQL_UPDATE_DOWNLOAD_FILESTATUS);
    			break;
    		case SmsAppConstant.FLAG_PARSE_RUNNING:
    			sql = params.getQueryProperties().getProperty(PropertiesConstant.SQL_UPDATE_PARSE_START_FILESTATUS);
    			break;
    		case SmsAppConstant.FLAG_PARSE_DONE:
    			sql = params.getQueryProperties().getProperty(PropertiesConstant.SQL_UPDATE_PARSE_END_FILESTATUS);
    			break;    			
    		default:
    			
    	}
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	
        Connection conn;
        PreparedStatement prepStmt = null;
        conn = SmsAppFactory.getDbConnection();
        prepStmt = conn.prepareStatement(sql);
        prepStmt.setString(1, filename);
        prepStmt.executeUpdate();
        prepStmt.close();
        conn.close();
    }
    
    public static boolean moveFile(String fullPathNameBefore, String fullPathNameAfter) {
    	boolean result = false;
    	File before = new File(fullPathNameBefore);
    	File after = new File(fullPathNameAfter+".DONE");
    	result = before.renameTo(after);
    	return result;
    }
    public static List getLocalFileList(PathSource p) {
    	String dir = p.getLocalPathDir();
    	String filter = p.getFileNamePattern();
    	if(dir==null) return null;
    	List fileList = new ArrayList();
    	File f = new File(dir);
    	File[] files = f.listFiles();
    	for(int i=0;i<files.length;i++) {
	    	if(!files[i].isDirectory()) {
	    		if(files[i].getName().indexOf(filter)!=-1) {
	    			
               		FileSource fs = new FileSource();
            		fs.setFileName(files[i].getName());
            		fs.setFileSize(files[i].length());
            		fs.setDownloadedTime(null);
            		fs.setSourcePathId(p.getPathSourceId());
            		fs.setLocalFilePath(dir);
            		fs.setCdrType(p.getCdrType());	    			
	    			fileList.add(fs);
	    		}
	    	}
    	}
    	return fileList;
    }
}
